package com.foreknow.elm.dao.impl;

import com.foreknow.elm.dao.CartDao;
import com.foreknow.elm.po.Business;
import com.foreknow.elm.po.Cart;
import com.foreknow.elm.po.Food;
import com.foreknow.elm.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CartDaoImpl implements CartDao {
    private Connection con = null;
    private PreparedStatement pst = null;
    private ResultSet rs = null;
    private int isRight = 0;
    @Override
    public List<Cart> dlistCart(String userid, Integer businessid) throws SQLException {

       List<Cart> list = new ArrayList<>();
       StringBuffer stringBuffer = new StringBuffer("select * from cart c,business b,food f where c.foodid = f.foodid and c.businessId = b.businessId and c.userid=?");
       con = DBUtil.getConnection();
       if (businessid != null){
           stringBuffer.append(" and c.businessId=?");
       }
       try {
           pst = con.prepareStatement(String.valueOf(stringBuffer));
           pst.setString(1,userid);
           if (businessid != null){
               pst.setInt(2,businessid);
           }
           rs = pst.executeQuery();
           while (rs.next()){
               Cart cart = new Cart();
               cart.setCartId(rs.getInt("cartId"));
               cart.setFoodId(rs.getInt("foodId"));
               cart.setBusinessId(rs.getInt("businessId"));
               cart.setUserId(rs.getString("userId"));
               cart.setQuantity(rs.getInt("quantity"));


               Food food = new Food();
               food.setFoodId(rs.getInt("foodId"));
               food.setFoodName(rs.getString("foodName"));
               food.setFoodExplain(rs.getString("foodExplain"));
               food.setFoodImg(rs.getString("foodImg"));
               food.setFoodPrice(rs.getDouble("foodPrice"));
               food.setBusinessId(rs.getInt("businessId"));
               food.setRemarks(rs.getString("remarks"));
               cart.setFood(food);

               Business business = new Business();
               business.setBusinessId(rs.getInt("businessId"));
               business.setBusinessName(rs.getString("businessName"));
               business.setBusinessAddress(rs.getString("businessAddress"));
               business.setBusinessExplain(rs.getString("businessExplain"));
               business.setBusinessImg(rs.getString("businessImg"));
               business.setOrderTypeId(rs.getInt("orderTypeId"));
               business.setStarPrice(rs.getDouble("starPrice"));
               business.setDeliveryPrice(rs.getDouble("deliveryPrice"));
               business.setRemarks(rs.getString("remarks"));
               cart.setBusiness(business);

               list.add(cart);
           }
       }finally {
           DBUtil.close(rs,pst);
       }

      return list;
    }

    @Override
    public int dsaveCart(Cart cart) throws SQLException {
        String sql = "insert into cart(userid,businessid,foodid,quantity) values(?,?,?,1)";
        con = DBUtil.getConnection();
        try {
            pst = con.prepareStatement(sql);
            pst.setString(1,cart.getUserId());
            pst.setInt(2,cart.getBusinessId());
            pst.setInt(3,cart.getFoodId());
            isRight = pst.executeUpdate();
        } finally {
            DBUtil.close(pst);
        }


        return isRight;
    }

    @Override
    public int dupdateCart(Cart cart) throws SQLException {
        String sql = "update Cart set quantity=? where userid=? and businessid=? and foodid=?";
        con = DBUtil.getConnection();
        try {
            pst = con.prepareStatement(sql);
            pst.setInt(1,cart.getQuantity());
            pst.setString(2,cart.getUserId());
            pst.setInt(3,cart.getBusinessId());
            pst.setInt(4,cart.getFoodId());
            isRight = pst.executeUpdate();
        }finally {
            DBUtil.close(pst);
        }
        return isRight;
    }

    @Override
    public int dremoveCart(Cart cart) throws SQLException {
        StringBuffer sql = new StringBuffer("delete from cart where userid=? and businessid=?");
        con = DBUtil.getConnection();
        try {
            if (cart.getFoodId() != null){
                sql.append(" and foodid=?");
            }
            pst = con.prepareStatement(String.valueOf(sql));
            pst.setString(1,cart.getUserId());
            pst.setInt(2,cart.getBusinessId());

            if (cart.getFoodId() != null){
                pst.setInt(3,cart.getFoodId());
            }
            isRight = pst.executeUpdate();
        }finally {
            DBUtil.close(pst);
        }
        return isRight;
    }
}
